/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package p2pscrabble;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 
 * @author fred0002
 */
public class BoardTile extends JPanel {
	// private Image tileImage;
	private LetterTile letterTile = null;
	private JLabel imageLabel;
	private ImageIcon tileImage;
	private boolean remove = true;
	private boolean thisTurn = false;
	private boolean lastTurn = false;

	private int lx;
	private int wx;

	public BoardTile(String imageFile, int col, int row, int lx, int wx) {
		this.lx = lx;
		this.wx = wx;

		this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

		tileImage = new ImageIcon(imageFile);
		imageLabel = new JLabel(tileImage);
		this.add(imageLabel);

		this.setPreferredSize(new java.awt.Dimension(
				BoardPanel.emptypanelheight, BoardPanel.emptypanelheight));
		repaint();

		this.setTransferHandler(new MyTransferHandler());
	}

	public void setLetterTile(char letter, boolean isBlank) {
		if (letterTile != null && letterTile.getLetter() == letter
				&& letterTile.isBlank() == isBlank)
			return;

		this.removeAll();
		letterTile = new LetterTile(letter, true, isBlank, false);
		this.add(letterTile);

		letterTile.setVisible(true);
		letterTile.setBounds(0, 0, 26, 26);
		thisTurn = true;
	}

	public char getLetter() {
		if (letterTile != null) {
			return letterTile.getLetter();
		} else
			return ' ';
	}

	public void removeTile() {
		this.removeAll();
		letterTile = null;
		this.add(imageLabel);
		repaint();
		thisTurn = false;
	}

	public boolean getThisTurn() {
		return thisTurn;
	}

	public boolean getRemove() {
		return remove;
	}

	public void setLastTurn(boolean b) {
		lastTurn = b;
	}

	public void setRemove(boolean rem) {
		remove = rem;
	}

	public int getLX() {
		return lx;
	}

	public int getWX() {
		return wx;
	}

	public void setTileFixed() {
		char l = letterTile.getLetter();
		boolean c = letterTile.isBlank();
		this.removeAll();
		letterTile = new LetterTile(l, true, c, true);
		this.add(letterTile);
		letterTile.setVisible(true);
		letterTile.setBounds(0, 0, BoardPanel.emptypanelheight,
				BoardPanel.emptypanelheight);

		if (thisTurn)
			lastTurn = true;
		thisTurn = false;
	}

	@Override
	public void paint(Graphics g) {
		if (lastTurn)
			this.setBorder(javax.swing.BorderFactory.createLineBorder(
					Color.RED, 1));
		else
			this.setBorder(null);
		super.paint(g);
	}
}
